package org.example.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.example.msmservice.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
   public boolean send(Map<String, Object> param, String phone){
       if (StringUtils.isEmpty(phone)) return false;
       DefaultProfile profile=DefaultProfile.getProfile("default","LTAI5tD4ZU42nPMrWTEYxvAU","rGdK4QiDNVleSBKqRCIMjej1r0jAOp");
       IAcsClient client=new DefaultAcsClient(profile);
       //设置参数
       CommonRequest request=new CommonRequest();
       request.setMethod(MethodType.POST);//请求方式
       request.setDomain("dysmsapi.aliyuncs.com");//访问阿里云哪个地方
       request.setVersion("2017-05-25");
       request.setAction("sendSms");
       request.putQueryParameter("PhoneNumbers",phone);
       request.putQueryParameter("SignName", "阿里云短信测试");//签名名称
       request.putQueryParameter("TemplateCode","SMS_154950909");//请求模板
       request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
       try{
           CommonResponse response=client.getCommonResponse(request);
           boolean success=response.getHttpResponse().isSuccess();
           return success;
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }

   }
}
